package gao.xiaolei.controller;

import java.util.List;
import javax.annotation.Resource;

import gao.xiaolei.dto.TestRequirementDesignCoverDto;
import gao.xiaolei.dto.TestRequirementDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.entity.TestRequirement;
import gao.xiaolei.exception.AuthException;
import gao.xiaolei.exception.StatusException;
import gao.xiaolei.service.TestRequirementService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/common/testRequirement")
public class TestRequirementController {

    @Resource(name = "TestRequirementService")
    private TestRequirementService testRequirementService;

    @PostMapping("")
    public void addTestRequirement(@RequestParam(name = "id", required = true) String id,
                                   @RequestParam(name = "title", required = true) String title,
                                   @RequestParam(name = "author", required = true) String author,
                                   @RequestParam(name = "status", required = true) int status,
                                   @RequestParam(name = "parentId", required = true) String parentId,
                                   @RequestParam(name = "project", required = true) String project,
                                   @RequestParam(name = "leaf", required = true) int leaf) {
        testRequirementService.addTestRequirement(id, title, author, status, parentId, project, leaf);
    }

    @DeleteMapping("/{id}/{userId}")
    public void deleteTestRequirement(@PathVariable(name = "id", required = true) String id,
                                      @PathVariable(name = "userId", required = true) String userId) {
        testRequirementService.deleteTestRequirement(id, userId);
    }

    @PutMapping("")
    public void updateTestRequirement(@RequestParam(name = "id", required = true) String id,
                                      @RequestParam(name = "title", required = true) String title,
                                      @RequestParam(name = "status", required = true) int status,
                                      @RequestParam(name = "userId", required = true) String userId) {
        testRequirementService.updateTestRequirement(id, title, status, userId);
    }

    @PutMapping("/title")
    public void updateTestRequirementTitle(@RequestParam(name = "id", required = true) String id,
                                           @RequestParam(name = "text", required = true) String title,
                                           @RequestParam(name = "userId", required = true) String userId) {
        testRequirementService.updateTestRequirementTitle(id, title, userId);
    }

    @PutMapping("/parent")
    public void updateParentId(@RequestParam(name = "id", required = true) String id,
                               @RequestParam(name = "newParentId", required = true) String newParentId,
                               @RequestParam(name = "userId", required = true) String userId) {
        testRequirementService.updateParentId(id, newParentId, userId);
    }

    @GetMapping("/project/{projectId}/{userId}")
    public List<TestRequirement> findByProject(@PathVariable(name = "projectId", required = true) String projectId,
                                               @PathVariable(name = "userId", required = true) String userId) {
        return testRequirementService.findByProject(projectId, userId);
    }

    @GetMapping("/children/{parentId}")
    public List<TestRequirement> getChildren(@PathVariable(name = "parentId", required = true) String parentId) {
        return testRequirementService.getChildren(parentId);
    }

    @GetMapping("/detail/{id}")
    public TestRequirementDto getDetail(@PathVariable(name = "id", required = true) String id)  {
        return testRequirementService.getDetail(id);
    }

    @GetMapping("/detail/cover/{id}")
    public TestRequirementDesignCoverDto getDetailForTestDesign(@PathVariable(name = "id", required = true) String id) {
        return testRequirementService.getDetailForTestDesign(id);
    }

    //获取该测试项目下通过审核的最顶端需求测试需求,
    @GetMapping("/project/{projectId}")
    public List<TestRequirement> findByProjectIsPass(@PathVariable(name = "projectId", required = true) String projectId) {
        return testRequirementService.getByProjectIsPass(projectId);
    }

    //将该测试需求转换成对应的测试计划
    @PostMapping("/transformToDesign")
    public void transformToDesign(@RequestParam(name = "id") String id,
                                  @RequestParam(name = "name") String name,
                                  @RequestParam(name = "isIncludeChildren") Integer isIncludeChildren,
                                  @RequestParam(name = "priority") Integer priority,
                                  @RequestParam(name = "subject") String subject,
                                  @RequestParam(name = "author") String author) {
        testRequirementService.transformTrqToDesign(id, name, isIncludeChildren, subject, priority, author);
    }

    @GetMapping("/title/{id}")
    public String getTitle(@PathVariable("id") String id) {
        return testRequirementService.getTitle(id);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AuthException.class)
            code = 401;
        else if (e.getClass() == StatusException.class)
            code = 402;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
